package car.slyc.controller;


import car.slyc.model.Gg;
import car.slyc.model.Xc;
import car.slyc.service.GgServiceImpl;
import car.slyc.util.JsonObject;
import car.slyc.util.PageRet;
import car.slyc.util.Time;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/gg")
public class GgController {

    @Resource
    private GgServiceImpl ggserviceimpl;


    //   查询最新的一条记录
    @RequestMapping("/qt/wuxgettop")
    @ResponseBody
    public JsonObject wuxgetinfo(@RequestParam(value = "id",defaultValue = "",required = false) String id){
        try {
            Gg gg=ggserviceimpl.getAlltop();
            return new JsonObject("200", "", gg);
        }
        catch (Exception e){
            e.printStackTrace();
            return new JsonObject("500", "", null);
        }
    }




    //   跳转 显示
    @RequestMapping("/tolist")
    public String tolist() {
        return "Gg/list";
    }

    //   跳转 添加
    @RequestMapping("/toadd")
    public String toadd(Model model) {
        model.addAttribute("modyid", "");
        return "Gg/add";
    }


    //   跳转 修改
    @RequestMapping("/toget")
    public String toget(String id, Model model) {
        System.out.println("修改的数据" + id);
        model.addAttribute("modyid", id);
        return "Gg/add";
    }

    //   添加
    @RequestMapping("/add")
    @ResponseBody
    public PageRet add(Gg gg, Model model) {
        gg.setId(UUID.randomUUID().toString().replace("-", ""));
        System.out.println(Time.getDate());
        gg.setTime(Time.getDate());
        if (ggserviceimpl.add(gg)>0){
            return new PageRet(200, "", null, 0);
        }else{
            return new PageRet(500, "", null, 0);
        }

    }

    //   修改
    @RequestMapping("/get")
    @ResponseBody
    public JsonObject getById(String id) {
        Gg gg = ggserviceimpl.getById(id);
        System.out.println(gg);
        return new JsonObject("200", "", gg);
    }


    //   修改
    @RequestMapping("/update")
    @ResponseBody
    public PageRet update(Gg gg) {

        System.out.println("前端传输的数据：" + gg);

       if (ggserviceimpl.update(gg)>0){
           return new PageRet(200, "", null, 0);
       }else{
           return new PageRet(500, "", null, 0);
       }


    }


    //   批量删除
    @RequestMapping("/del")
    @ResponseBody
    public PageRet dels(@RequestParam("id") String ids) {

        System.out.println("传递过来的数据：" + ids.toString());

        if (ggserviceimpl.dels(ids) > 0) {
            return new PageRet(200, "成功", null, 0);
        } else {
            return new PageRet(500, "失败", null, 0);
        }
    }

    //   分页显示所有
    @RequestMapping(value = "/pagegetall")
    @ResponseBody
    public PageRet list(@RequestParam(value = "page", defaultValue = "1", required = false) int page,
                        @RequestParam(value = "limit", defaultValue = "10", required = false) int limit) {

        //  查询之前调用    传入 当前页码、每页条数  及排序方法
        PageHelper.startPage(page, limit, " time desc");

        //startPage 后面紧跟的查询是分页查询
        List<Gg> list = ggserviceimpl.getall();

        //用PageInfo 对结果进行包装，传入连续显示的页数
        PageInfo<Gg> all = new PageInfo<>(list);

        System.out.println("pageinfo包装后的数据" + all.getList());
        return new PageRet(0, "", all.getList(), (int) all.getTotal());
    }

}
